package com.second.controller.storeInfo;

import com.second.consts.PersonalConst;
import com.second.mapper.FMessageMapper;
import com.second.mapper.FShopDao;
import com.second.mapper.FUserMapper;
import com.second.model.dto.reply.FReplyDto;
import com.second.model.dto.reviews.FUserCommentDto;
import com.second.model.dto.storeInfo.FShopFractionDto;
import com.second.model.pojo.*;
import com.second.moudle.Result;
import com.second.service.*;
import com.second.websocket.WebSocketProcess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 本类用来演示:
 *
 * @author: MuTing
 * @date: 2022-04-14 21:47
 */
@Controller
@RequestMapping("storeInfo")
public class StoreInfoController {

    @Autowired
    private IFShopService shopService;

    @Autowired
    private IFGoodsService goodsService;

    @Autowired
    private IFShopPictureService shopPictureService;
    
    @Autowired
    private IFCommentService commentService;

    @Autowired
    private FUserMapper userMapper;

    @Autowired
    private FShopDao shopDao;

    @Autowired
    private FMessageMapper messageMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IFReplyService replyService;

    @Autowired
    private WebSocketProcess webSocketProcess;

    @GetMapping("enter/{id}")
    public String enter(
            @PathVariable(name = "id") Integer id,
            HttpServletRequest request){

        FShop fShop = shopService.getById(id);

        int countComment = commentService.CountComments(id);

        FShopFractionDto fShopFractionDto = commentService.CommentScoreCounts(id);
        String coordinate = shopService.coordinate(id); //高德坐标

        List<FGoods> fGoodslist = goodsService.findAllByShopId(id);
        List<FShop> shops = shopService.findShopsByTypes(id,fShop.getType());

        request.setAttribute("coordinate",coordinate);
        request.setAttribute("fShop",fShop);
        request.setAttribute("shops",shops);
        request.setAttribute("fGoodslist",fGoodslist);
        request.setAttribute("countComment",countComment);
        request.setAttribute("fShopFractionDto", fShopFractionDto);
        return "jsp/storeInfo/storeInfo";
    }

    @ResponseBody
    @PostMapping("picture/{shopId}")
    public Result pictureUrl(@PathVariable(name = "shopId") Integer shopId){
        FShopPicture fShopPicture = shopPictureService.getPictureByShopId(shopId);
        return new Result("200","图片获取成功！",fShopPicture);
    }

    @ResponseBody
    @PostMapping("comments/{shopId}/{pageNumber}")
    public Result comments(@PathVariable(name = "shopId") Integer shopId,@PathVariable(name = "pageNumber") Integer pageNumber){
        if ( null == pageNumber) {
            pageNumber = 1;
        }
        List<FUserCommentDto> fCommentList = commentService.findCommentsByShopId(shopId,pageNumber-1);

        return new Result("200","评论获取成功！",fCommentList);
    }

    @ResponseBody
    @PostMapping("replies/{commentId}/{replyId}")
    public Result replies(@PathVariable(name = "commentId") Integer commentId,@PathVariable(name = "replyId") Integer replyId){
        System.out.println(commentId);
        List<FReplyDto> frd = new ArrayList<>();
        if (null == replyId){
            frd = replyService.findByCommentIdAndReplyId(commentId,0);
        }else {
            frd = replyService.findByCommentIdAndReplyId(commentId,replyId);
        }

        return new Result("200","回复获取成功!",frd);
    }

    @ResponseBody
    @PostMapping("reply")
    public Result reply(@RequestBody FReply fReply){
        replyService.addReply(fReply);
        //添加回复的时候发送消息
        FMessage message = new FMessage();
        Integer userId = commentService.findById(fReply.getCommentId()).getUserId();
        message.setUserId(userId);
        message.setReplyTime(fReply.getReplyTime());
        message.setUserName(userMapper.findById(userId).get().getUsername());
        message.setShopName(shopDao.findById(commentService.findById(fReply.getCommentId()).getShopId()).get().getShopName());
        messageMapper.save(message);
        try {
            //发送给发表此评论的用户id消息
            webSocketProcess.sendMessage(userId, "收到回复");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result("200","回复成功！");
    }

    /**
     * 点赞的时候将取消点赞redis中移除
     * 然后放到点赞的redis中
     * @param userId
     * @param commentId
     * @return
     */
    @PostMapping("doZan/{userId}/{commentId}")
    @ResponseBody
    public Result doZan(@PathVariable(name = "userId") Integer userId,
                        @PathVariable(name = "commentId") Integer commentId){
        String keyZan = "Zan:"+commentId;
        String valueZan = userId+"::"+ PersonalConst.Zan;
        String keyNotZan = "NotZan:"+commentId;
        String valueNotZan = userId+"::"+ PersonalConst.Not_Zan;
        //移除取消点赞的redis
        redisTemplate.opsForSet().remove(keyNotZan,valueNotZan);
        //添加点赞的redis
        long code = redisTemplate.opsForSet().add(keyZan,valueZan);
        if (code == 1){
            return new Result("200","点赞成功");
        }else {
            return new Result("400","您已重复点赞");
        }
    }

    /**
     * 取消点赞的时候将点赞redis中移除
     * 然后放到取消点赞的redis中
     * @param userId
     * @param commentId
     * @return
     */
    @PostMapping("cancelZan/{userId}/{commentId}")
    @ResponseBody
    public Result cancelZan(@PathVariable(name = "userId") Integer userId,
                        @PathVariable(name = "commentId") Integer commentId){

        String keyZan = "Zan:"+commentId;
        String valueZan = userId+"::"+ PersonalConst.Zan;
        String keyNotZan = "NotZan:"+commentId;
        String valueNotZan = userId+"::"+ PersonalConst.Not_Zan;
        //移除点赞的redis
        long code1 = redisTemplate.opsForSet().remove(keyZan,valueZan);
        //添加取消点赞的redis
        long code = redisTemplate.opsForSet().add(keyNotZan,valueNotZan);
        if (code == 1 && code1 == 1){
            return new Result("200","取消点赞成功");
        }else {
            return new Result("400","您已重复取消点赞");
        }
    }

    @PostMapping("zanCount/{commentId}")
    @ResponseBody
    public Result zanCount(@PathVariable(name = "commentId") Integer commentId){
        String key = "Zan:"+commentId;
        long size = redisTemplate.opsForSet().size(key);
        return new Result("200","获赞数量",size);
    }
}

